
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>06. 泛型 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="05.function.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    00. 简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01.typescript-in-5-minutes.html">
            
                <a href="01.typescript-in-5-minutes.html">
            
                    
                    01. 5 分钟上手
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="02. basic-types.md">
            
                <span>
            
                    
                    02. 基础类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="03.interface.html">
            
                <a href="03.interface.html">
            
                    
                    03. 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="04.class.html">
            
                <a href="04.class.html">
            
                    
                    04. 类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="05.function.html">
            
                <a href="05.function.html">
            
                    
                    05. 函数
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.7" data-path="06.generics.html">
            
                <a href="06.generics.html">
            
                    
                    06. 泛型
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >06. 泛型</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x80CC;&#x666F;">&#x80CC;&#x666F;</h2>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x8FD4;&#x56DE;&#x4EFB;&#x4F55;&#x4F20;&#x5165;&#x5B83;&#x7684;&#x503C;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x80FD;&#x4F1A;&#x8FD9;&#x6837;&#x505A;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">identity</span>(<span class="hljs-params">arg: <span class="hljs-built_in">any</span></span>): <span class="hljs-title">any</span> </span>{
    <span class="hljs-keyword">return</span> arg;
}
</code></pre>
<p>&#x8FD9;&#x91CC;&#x6709;&#x4E00;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x4FBF;&#x662F;&#x6211;&#x4EEC;&#x4E0D;&#x80FD;&#x4E25;&#x683C;&#x63A7;&#x5236;&#x5165;&#x53C2;&#x4E0E;&#x51FA;&#x53C2;&#x4FDD;&#x6301;&#x4E00;&#x81F4;&#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x7528;&#x5230;&#x6CDB;&#x578B; <code>generics</code></p>
<h2 id="&#x6CDB;&#x578B;">&#x6CDB;&#x578B;</h2>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">identity</span>&lt;<span class="hljs-title">T</span>&gt;(<span class="hljs-params">arg: T</span>): <span class="hljs-title">T</span> </span>{
    <span class="hljs-keyword">return</span> arg;
}
</code></pre>
<p>&#x8FD9;&#x662F;&#x6CDB;&#x578B;&#x51FD;&#x6570;&#x7684;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;&#xFF0C;&#x4F7F;&#x7528;&#x65B9;&#x5F0F;&#x5219;&#x6709;&#x5982;&#x4E0B;&#x4E24;&#x79CD;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> output = identity&lt;<span class="hljs-built_in">string</span>&gt;(<span class="hljs-string">&quot;myString&quot;</span>);  <span class="hljs-comment">// type of output will be &apos;string&apos;</span>
</code></pre>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> output = identity(<span class="hljs-string">&quot;myString&quot;</span>);  <span class="hljs-comment">// type of output will be &apos;string&apos;</span>
</code></pre>
<ul>
<li>&#x6211;&#x4EEC;&#x6CA1;&#x5FC5;&#x8981;&#x4F7F;&#x7528;&#x5C16;&#x62EC;&#x53F7;&#xFF08;<code>&lt;&gt;</code>&#xFF09;&#x6765;&#x660E;&#x786E;&#x5730;&#x4F20;&#x5165;&#x7C7B;&#x578B;&#xFF1B;&#x7F16;&#x8BD1;&#x5668;&#x53EF;&#x4EE5;&#x67E5;&#x770B;<code>myString</code>&#x7684;&#x503C;&#xFF0C;&#x7136;&#x540E;&#x628A;<code>T</code>&#x8BBE;&#x7F6E;&#x4E3A;&#x5B83;&#x7684;&#x7C7B;&#x578B;</li>
</ul>
<h3 id=""> </h3>
<h3 id="&#x6CDB;&#x578B;&#x6570;&#x7EC4;">&#x6CDB;&#x578B;&#x6570;&#x7EC4;</h3>
<pre><code class="lang-ts">function loggingIdentity&lt;T&gt;(arg: T[]): T[] {
    console.log(arg.length);  // Array has a .length, so no more error
    return arg;
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="05.function.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 05. 函数">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"06. 泛型","level":"1.7","depth":1,"previous":{"title":"05. 函数","level":"1.6","depth":1,"path":"05.function.md","ref":"05.function.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"06.generics.md","mtime":"2018-10-09T09:39:00.323Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-10-10T12:13:20.554Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

